import 'package:flutter/services.dart';

/// Phone number input formatter that allows international phone number format
/// Supports format like: +2541234567890
class PhoneNumberInputFormatter2 extends TextInputFormatter {

  static int length = 11;

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    final text = newValue.text;
    
    // Allow empty text
    if (text.isEmpty) {
      return newValue;
    }

    // Remove any non-digit characters except the leading +
    final digitsOnly = text.replaceAll(RegExp(r'[^\d]'), '');
    
    // Limit to reasonable phone number length (15 digits after +)
    if (digitsOnly.length > length) { // + + 15 digits
      return oldValue;
    }
    
    return TextEditingValue(
      text: digitsOnly,
      selection: TextSelection.collapsed(offset: digitsOnly.length),
    );
  }
}
